from PyQt5.QtWidgets import (QHBoxLayout, QToolButton, QLabel)
from PyQt5.QtCore import Qt, QPoint, QSize
from PyQt5.QtGui import QPainter, QIcon, QPaintEvent, QMouseEvent
from PyQt5.QtWidgets import QColorDialog
from PyQt5.QtCore import QSettings
from core.themecolor import ThemeColor
from core.floatwidget import FloatWidget

class MenuBar(FloatWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_aboutUs = None
        self.m_dragPosition = QPoint()
        self.m_movePosition = QPoint()
        self.m_isMove = False

        self.setup_ui()

    def setup_ui(self):
        self.setFixedHeight(40)

        # Right side buttons
        right_layout = QHBoxLayout()
        right_layout.setSpacing(5)
        right_layout.setContentsMargins(0, 5, 20, 0)

        # About button
        self.about = QToolButton(self)
        self.about.setAutoRaise(True)
        self.about.setFixedSize(32, 32)
        self.about.setIconSize(QSize(18, 18))
        self.about.setIcon(QIcon("pic/about.png"))
        self.about.clicked.connect(self.show_about)

        # Skin button
        self.skin = QToolButton(self)
        self.skin.setAutoRaise(True)
        self.skin.setFixedSize(32, 32)
        self.skin.setIconSize(QSize(18, 18))
        self.skin.setIcon(QIcon("pic/skin.png"))
        self.skin.clicked.connect(self.change_skin)

        # Minimize button
        self.min = QToolButton(self)
        self.min.setAutoRaise(True)
        self.min.setFixedSize(32, 32)
        self.min.setIconSize(QSize(16, 16))
        self.min.setIcon(QIcon("pic/min.png"))

        # Normal/Maximize button
        self.normal = QToolButton(self)
        self.normal.setAutoRaise(True)
        self.normal.setFixedSize(32, 32)
        self.normal.setIconSize(QSize(16, 16))
        self.normal.setIcon(QIcon("pic/normal.png"))

        # Close button
        self.close_btn = QToolButton(self)
        self.close_btn.setAutoRaise(True)
        self.close_btn.setFixedSize(32, 32)
        self.close_btn.setIconSize(QSize(16, 16))
        self.close_btn.setIcon(QIcon("pic/close.png"))

        # Connect buttons to parent window actions
        parent_widget = self.parent()
        self.min.clicked.connect(parent_widget.showMinimized)
        self.normal.clicked.connect(self.toggle_maximize)
        self.close_btn.clicked.connect(parent_widget.close)

        # Add buttons to right layout
        right_layout.addWidget(self.about)
        right_layout.addWidget(self.skin)
        right_layout.addWidget(self.min)
        right_layout.addWidget(self.normal)
        right_layout.addWidget(self.close_btn)

        # Main layout
        main_layout = QHBoxLayout()
        main_layout.setContentsMargins(10, 0, 0, 0)
        main_layout.setSpacing(0)

        self.titleLabel = QLabel("PyAutoGUI 自动化脚本生成器 (ZhangXL31313)", self)
        self.titleLabel.setStyleSheet("font-size:20px;")
        main_layout.addWidget(self.titleLabel)
        main_layout.addStretch()
        main_layout.addLayout(right_layout)

        self.setLayout(main_layout)

    def show_about(self):
        if not self.m_aboutUs:
            from core.aboutus import AboutUS
            self.m_aboutUs = AboutUS()
        self.m_aboutUs.show()

    def change_skin(self):
        color = QColorDialog.getColor(ThemeColor.baseColor(), self, "Select color")
        if color.isValid():
            ThemeColor.setBaseColor(color)
            settings = QSettings("setting.ini", QSettings.IniFormat)
            settings.setValue("bgColor", color)

    def toggle_maximize(self):
        parent_widget = self.parent()
        if parent_widget.isMaximized():
            parent_widget.showNormal()
            self.normal.setIcon(QIcon("pic/max.png"))
        else:
            parent_widget.showMaximized()
            self.normal.setIcon(QIcon("pic/normal.png"))

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.m_dragPosition = event.pos()
            self.m_isMove = True

    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() & Qt.LeftButton and self.m_isMove:
            parent_widget = self.parent()
            self.m_movePosition = event.globalPos()

            if parent_widget.isMaximized():
                parent_widget.showNormal()
                self.m_dragPosition = event.pos()
                self.normal.setIcon(QIcon("pic/max.png"))
                return

            parent_widget.move(self.m_movePosition - self.m_dragPosition)

    def mouseReleaseEvent(self, event: QMouseEvent):
        self.m_isMove = False

    def mouseDoubleClickEvent(self, event: QMouseEvent):
        parent_widget = self.parent()
        if parent_widget.isMaximized():
            parent_widget.showNormal()
            self.normal.setIcon(QIcon("pic/normal.png"))
        else:
            parent_widget.showMaximized()
            self.normal.setIcon(QIcon("pic/max.png"))

    def paintEvent(self, event: QPaintEvent):
        painter = QPainter(self)
        color = ThemeColor.baseColor()
        color.setAlphaF(0.1)
        painter.fillRect(event.rect(), color)

    def setWindowTitle(self, title: str):
        self.titleLabel.setText(title)